﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using Etalk.Library;
namespace EtalkServer
{
    public partial class FrmMain
    {
       //添加用户
        private Client BindClient(Socket newClientSoc,string[] data)
        {
           Client client = AllSocket.FindByKey(data[2].ToString());
           if (client != null)
               return client;
            string ip = ((IPEndPoint)newClientSoc.RemoteEndPoint).Address.ToString();
            //添加到Client集合
            Client cItem = new Client(data[2].ToString(), clientThread, newClientSoc,data[1].ToString(), ip);
            AllSocket.Add(cItem);

            statusBar1.BeginInvoke(new EventStatusValue(SetStatusValue), txtClientCount, AllSocket.List.Count.ToString());
            return cItem;
        }

        //移除
        private void RemoveClient(Client clientObj)
        {
            //移除控件信息
            if (clientObj == null)
                return;

            listClient.Invoke(new EventRemoveItem(RemoveItem), listClient, clientObj.Host.ToString());
            statusBar1.BeginInvoke(new EventStatusValue(SetStatusValue), txtClientCount, (AllSocket.List.Count-1).ToString());
            //终止线程和关闭连接Socket
            Thread threadObj = clientObj.CLThread;
            clientObj.Sock.Close();
            AllSocket.Remove(clientObj);
            threadObj.Abort();
            
        }


        #region 命令执行 方法
        //登录命令
        private void CommandLogin(Client clientObj, string[] data)
        {
            try
            {
                string loginName = data[2];
                string password = data[3];
                if (ClientUser.CheckLogin(loginName, password))
                {
                    ListViewItem viewItem = new ListViewItem(
                       new string[]{clientObj.IP,
                             DateTime.Now.ToString(),
                             data[1],
                             data[2]
                             });
                    viewItem.Tag = clientObj.Host.ToString();
                    viewItem.ImageIndex = -1;
                    //添加到控件列表
                    this.Invoke(new EventAddListViewItem(AddViewItem), listClient, viewItem);                    

                   Comm.SendDataToClient(clientObj, "登录成功");
                }
                else
                {
                    Comm.SendDataToClient(clientObj, "登录失败");
                    RemoveClient(clientObj);
                }
            }
            catch (Exception ex)
            {
                
            }
        }
        //guest 连接
        private void CommandConn(Client clientObj, string[] data)
        {
         
            ListViewItem viewItem = new ListViewItem(
                new string[]{clientObj.IP,
                             DateTime.Now.ToString(),
                             data[1],
                             data[2]
                             });
            viewItem.Tag = clientObj.Host.ToString();
            viewItem.ImageIndex = -1;
            this.Invoke(new EventAddListViewItem(AddViewItem), listClient, viewItem);
        }
        //私聊
        private void CommandPriv(Client clientObj, string[] data)
        {
            foreach (Client item in AllSocket.List)
            {
                if (item.Name.Trim() == data[1].ToString())
                {
                    Comm.SendDataToClient(item, data[2].ToString());
                    break;
                }
            }
        }

        #endregion
      
        

        private void Trace(string content)
        {
            this.BeginInvoke(new EventHandler(delegate(object sender,EventArgs e)
                {
                    rtbTrace.AppendText(Environment.NewLine + content);
                    rtbTrace.ScrollToCaret();
                }));
        }
    }
}
